# -*- coding:utf-8 -*-
from datetime import datetime
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import time
from appUtils import WaitSecond, browser_changeTopWindow, browser_doWait, waitInfiniti, getWebDriver, logProxyContent

LoginName = "3050933250"
LoginPd = "Hrg123456"


def loadData(webProxyDriver):
    (browser, _, _) = webProxyDriver
    result = [];
    # 登录
    url = "http://open.qq.com/login?from=http%3A%2F%2Fop.open.qq.com%2Fmobile_appinfov2%2Fdetail%3Fappid%3D1106876590"
    browser.get(url)
    browser_doWait(browser)
    iframe = browser.find_element_by_id("login_frame");
    browser.switch_to.frame(iframe)
    browser.find_element_by_id("switcher_plogin").click()
    browser_doWait(browser)
    # 填充用户名密码
    browser.find_element_by_id("uin_del").click() #清除用户名
    browser.find_element_by_id("u").send_keys(LoginName)
    browser.find_element_by_id("p").send_keys(LoginPd)
    browser.find_element_by_id("login_button").click()
    browser_doWait(browser)
    # 跳过引导页
    browser_changeTopWindow(browser)
    browser.maximize_window()
    # WebDriverWait.until(EC.presence_of_element_located((By.CLASS_NAME,"guide-btn")))
    try:
        browser.find_element_by_css_selector("a[class='guide-btn cursor-pt']").click()
        browser.find_element_by_css_selector("a[class='guide-close cursor-pt']").click()
        browser.find_element_by_css_selector("a[class='ui-btn-blue']").click()
        browser_doWait(browser)
        iframe = browser.find_element_by_id("ifm-ability");
        browser.switch_to.frame(iframe)
        browser_doWait(browser)
        browser.find_element_by_class_name("guide-btn").click()
        browser.find_element_by_class_name("guide-btn").click()
        browser.find_element_by_class_name("guide-btn").click()
        browser.find_element_by_class_name("guide-btn").click()
        browser.find_element_by_class_name("guide-btn").click()
    except Exception as e:
        browser.get("http://op.open.qq.com/mobile_appinfov2/detail?appid=1106876590&v=2")
    browser_changeTopWindow(browser)
    try:
        element_j_header = browser.find_element_by_id("j-main-header").find_element_by_tag_name("a")
        element_j_header.click()
        browser_changeTopWindow(browser)
    except:
        pass
    # 请求当月的数据
    browser.find_element_by_class_name("option-list").find_elements_by_tag_name("li")[1].click()
    # 解析数据
    svg = browser.find_element_by_tag_name("svg")
    pathRoot = svg.find_element_by_css_selector("g[class='highcharts-markers highcharts-series-0 highcharts-tracker']")
    paths = pathRoot.find_elements_by_tag_name("path")
    for item in paths:
        # 鼠标悬停事件
        ActionChains(browser).move_to_element(item).perform()
        tip = svg.find_element_by_css_selector("g[class='highcharts-tooltip']")
        targetText = tip.find_element_by_tag_name("text").text;
        # 12月29日● 下载量: 0次
        # print(targetText)
        if (len(targetText) > 0):
            _date = targetText[:targetText.find("●")]
            _timeStr = targetText[targetText.find(":") + 2:len(targetText) - 1]
            result.append((formateDate(_date), _timeStr))
        time.sleep(1)
    # browser.quit()
    print("qq",result)
    return result


def formateDate(date):
    """
    12月29日转成2018-12-30
    :param date:
    :return:
    """
    _now = datetime.now()
    _nowTuple = _now.timetuple()
    _year = _nowTuple.tm_year
    _md = date[:date.find("月")] + "-" + date[date.find("月") + 1:len(date) - 1]
    _dateStr = str(_year) + "-" + _md
    _date = datetime.strptime(_dateStr, "%Y-%M-%d")
    if (_date > _now):
        _dateStr = str(_year - 1) + "-" + _md
    # print(_dateStr)
    return _dateStr

# formateDate("12月29日")
# loadData()
